package biz.yfsoft.app.fastframework.admin;

import biz.yfsoft.app.fastframework.Constant;

import com.jfinal.aop.ClearInterceptor;
import com.jfinal.aop.ClearLayer;
import com.jfinal.kit.EncryptionKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class IndexController extends AdminController {
	private Record user;
	
	@ClearInterceptor(ClearLayer.ALL)
	public void doLogin() {
		String login_name = getPara("username");
		user = Db.findFirst(
				"select * from sys_user where login_name=?",login_name);
		if (user == null) {
//			json.put("error", 1);
//			json.put("msg", "用户名或密码错误~");
		} else {
			String login_pass = getPara("password");
			String passEncode = user.get("pass_encode");
			if(StrKit.notBlank(passEncode)){
				login_pass = EncryptionKit.encrypt(passEncode, login_pass);
				if(login_pass.equals(user.get("login_pass"))){
					//验证成功
					setSessionAttr(Constant.KEY_ADMIN_USER, user);
				}
			}
		}
		redirect("/admin/index");
	}
	
	@ClearInterceptor(ClearLayer.ALL)
	public void logout() {
		removeSessionAttr(Constant.KEY_ADMIN_USER);
		redirect("/admin/login");
	}
	
	@ClearInterceptor(ClearLayer.ALL)
	public void login() {
		render("/admin/login.jsp");
	}
	
	public void index() {
		render("/admin/dashboard.jsp");
	}
	
	public void dashboard() {
		render("/admin/dashboard.jsp");
	}
	
	public void users() {
		render("/admin/users/list.jsp");
	}
}
